/* ==================================================================
* Copyright (c) 2024/11/15 SCNU LiuJunJie z-ken
* All rights reserved.
* fileName:	Base.hpp
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the xxx Group. and
* its contributors.
* 4. Neither the name of the Group nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Author,GROUP AND CONTRIBUTORS
* ===================================================================
* Author: LiuJunJie z-ken.
*/

/**
*@ time:	2024-10-06 
*@ update:	增加NFA的扩容机制，修复逻辑
*/
/**
*@ time:	2024-11-06 
*@ update:	测试tiny，删去测试文件位于git中的误区
*/
/**
*@ time:    2024-11-14	
*@ update:	修复部分bug，将joint.cpp的输出方式修改，可以为双文件模式，也可以为单文件
        支持tiny和minic
*/

#pragma once
#include "libs.hpp"

#if !defined(BASE_HPP)
#define BASE_HPP

// 正则表达式单位
class Base
{
public:
    Base()
    {
    }
    Base(string name1, string re) : name(name1), regular_Expression(re)
    {
    }
    int row, col; // 位于编码文件的行列位置
    string name = "0"; // 正则表达式对应的名称
    string regular_Expression; // 正则表达式的内容，如果是复合的正则表达式，需要使用字符串，否则此字符串为单个字符
};

class BaseUtils
{
public:
    // TODO 在这里进行修复preParse的问题
    static string parseComplexRe(string complexRe)
    {
        string s = complexRe;
        vector<string> punc_s({"|", "*", "+", "?", "(", ")"}); // 运算符号
        unordered_set<string> punc = unordered_set<string>(punc_s.begin(), punc_s.end());
        string parsed_s = "";
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '[')
            {
                parsed_s += "(";
                continue;
            }
            else if (s[i] == ']')
            {
                if (parsed_s[parsed_s.size() - 1] == '|')
                {
                    parsed_s.pop_back();
                }
                parsed_s += ")";

                break;
            }
            else if (s[i] == ',' || punc.find(s.substr(i, 1)) != punc.end()) // 屏蔽
            {
                continue;
            }
            else if (s[i] == '-') // 运算
            {
                if (i - 1 >= 0 && i + 1 < s.size())
                {
                    char c1 = s[i - 1];
                    char c2 = s[i + 1];
                    for (int i = c1 + 1; i < c2; i++)
                    {
                        parsed_s += string({(char)i, '|'});
                    }
                }
                continue;
            }
            else if (s[i] == '\\' && i + 1 < s.size())
            {
                parsed_s += string({s[i], s[i + 1], '|'});
                i += 1;
                continue;
            }
            parsed_s += string({s[i]});
            parsed_s += "|";
        }

        cout << "parseComplexRe: return " << parsed_s << endl;
        return parsed_s;
    }
};

#endif // BASE_HPP
